import { /** login, */ logout, getInfo } from '@/api/user'
import { getToken, /** setToken, */ getRefreshToken, /** setRefreshToken, */ removeToken, removeRefreshToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    publicRoute: 'admin',
    name: '',
    mobile: '',
    user_info: null,
    refreshToken: getRefreshToken(),
    menusList: []
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_REDRESHTOKEN: (state, refreshToken) => {
    state.refreshToken = refreshToken
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_INFO: (state, info) => {
    state.user_info = info
  },
  SET_MOBILE: (state, mobile) => {
    state.mobile = mobile
  },
  SET_MENUSLIST: (state, menusList) => {
    state.menusList = menusList
  }
}

const actions = {
  // 性能监控
  toggleMenusList({ commit }, menusList) {
    commit('SET_MENUSLIST', menusList)
  },
  // token
  toggleSetToken({ commit }, obj) {
    commit('SET_TOKEN', obj)
  },
  // refreshToken
  toggleRefreshToken({ commit }, obj) {
    commit('SET_REDRESHTOKEN', obj)
  },
  // user login
  // login({ commit }, userInfo) {
  //   const { username, password, captcha_token } = userInfo
  //   return new Promise((resolve, reject) => {
  //     login({ username: username.trim(), password: password, captcha_token: captcha_token }).then(response => {
  //       const { data } = response
  //       commit('SET_TOKEN', data.token_type + ' ' + data.access_token)
  //       setToken(data.token_type + ' ' + data.access_token)
  //       commit('SET_REDRESHTOKEN', data.refresh_token)
  //       setRefreshToken(data.refresh_token)
  //       resolve()
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response
        if (!data) {
          return reject('验证失败，请重新登录。')
        }
        const { name, mobile } = data
        commit('SET_NAME', name)
        commit('SET_MOBILE', mobile)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        removeRefreshToken()
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      removeRefreshToken()
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

